<?php
/**
 * 发送验证码类
 * @authors leqing Cao (caoleqing@qq.com)
 * @date    2017-10-18 14:45:06
 * @link 	http://www.cnblogs.com/leqing/
 */
namespace app\api\controller;
use phpmailer\PHPMailer;
use submail\messagexsend;

class Code extends Common {

	public function get_code() {
		/*接收参数*/
		$data = $this->params;
		/*检测用户名*/
		$user_name_type = $this->check_username($data['user_name']);
		switch ($user_name_type) {
		case 'phone':
			/*发送短信验证码*/
			$this->send_by_code($data['user_name'], 'phone', intval($data['is_exist']));
			break;

		case 'email':
			$this->send_by_code($data['user_name'], 'email', intval($data['is_exist']));
			break;
		}
	}
	/**
	 * 验证发送邮箱验证码还是短信验证码
	 * @param  [string] $username [手机号/邮箱]
	 * @param  [string] $type     [phone/email]
	 * @param  [int] $is_exist 	[0/1]
	 * @return [json]           [最终返回结果]
	 */
	public function send_by_code($username, $type, $is_exist) {
		if ($type == 'phone') {
			$send_type = '手机';
		} else {
			$send_type = '邮箱';
		}
		/*检测邮箱/手机是否存在*/
		$this->check_exites($username, $type, $is_exist);
		/*检测请求验证码频率*/
		if (session("?" . md5($username . '_last_send_time'))) {
			if (time() - session(md5($username . '_last_send_time')) < 60) {
				$this->return_msg(400009, $send_type . '验证码请求过于频繁，请稍后再试');
			}
		}
		/*生成验证码*/
		$code = $this->make_code(6);
		/*验证码存入session*/
		session(md5($username . '_last_send_code'), $code); #存入验证码
		session(md5($username . '_last_send_time'), time());
		if ($type == 'phone') {
			$this->send_to_code_phone($username, $code);
		} else {
			$this->send_to_code_email($username, $code);
		}
	}
	/**
	 * 生成验证码方法
	 * @param  [int] $num [验证码的位数]
	 * @return [int]      [最终生成的验证码]
	 */
	public function make_code($num) {
		$max = pow(10, $num) - 1;
		$min = pow(10, $num - 1);
		return rand($min, $max);
	}
	/**
	 * 发送手机验证码
	 * @param  [int(11)] $phone [手机号]
	 * @param  [int] $code  [验证码]
	 * @return [json]        [短信发送结果]
	 */
	public function send_to_code_phone($phone, $code) {
		$submail = new MESSAGEXsend();
		$submail->setTo($phone);
		$submail->SetProject('CZqIw1');
		$submail->AddVar('code', $code);
		$submail->AddVar('time', 60);
		$xsend = $submail->xsend();
		if ($xsend['status'] !== 'success') {
			$this->return_msg(400010, $xsend['msg']);
		} else {
			$this->return_msg(200, '短信发送成功,请注意查收');
		}
	}
	/**
	 * 邮件发送验证码
	 * @param  [string] $email [收件人邮箱]
	 * @param  [int] $code  [待发送的验证码]
	 * @return [json]        [最终发送结果]
	 */
	public function send_to_code_email($email, $code) {
		$phpmailer = new PHPMailer();
		$phpmailer->IsSMTP();
		$phpmailer->CharSet = 'utf-8';
		$phpmailer->Host = 'smtp.qq.com';
		$phpmailer->SMTPAuth = true;
		$phpmailer->Username = 'caoleqing@qq.com';
		$phpmailer->Password = 'Cyt20150401';
		$phpmailer->SMTPSecure = 'ssl';
		$phpmailer->Port = 465;
		$phpmailer->setFrom('caoleqing@qq.com', '零点科技');
		$phpmailer->addAddress($email, '零点科技验证码');
		$phpmailer->addReplyTo('cao.leqing@fosunhani.com', 'Reply');
		$phpmailer->Subject = '您收到一封验证码邮件';
		$phpmailer->Body = "[零点科技] 您的验证码是$code,请在60秒内输入。如非本人操作，请忽略该邮件！";
		if ($phpmailer->send()) {
			$this->return_msg(200, '验证码已发送成功,请注意查收');
		} else {
			$this->return_msg(400010, $phpmailer->ErrorInfo);
		}
	}
}